#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
use strict;
use CGI qw(param);
use SIM_settings;
use SIM_base;
use SIM_cgi;
use SNMP_util;

# Startupscript casetool, part of Sipman.

my $tool = $ARGV[0];
my $action = $ARGV[1];
my $option1 = $ARGV[2];
if ( "$option1" eq "none" ) {
  $option1 = "";
}
my $option2 = $ARGV[3];
my $option3 = $ARGV[4];


my $httpReF = $ENV{'HTTP_REFERER'};
my $httpScriptName = $ENV{'httpScriptName'};
my $systemHtml = $ENV{'SIM_SYSTEM'} || die "Oops no env found for SIM_SYSTEM.";

sub displayMenu{
  print("<FONT COLOR=#666666>");
  printMenu("mon","line", $tool, $action, "$option1+$option2+$option3");
  print("</FONT></TD></TR></TABLE><CENTER>");
}

sub readUpdParam {
  my $workFile = "@_[0]";
  my $name = "@_[1]";
  my $type = "@_[2]";
  my $param = param("$name");
  if ( "$type" eq "fill" ) {
    $param=~s/ /_/g;
  }
  if ( "$param" eq "" ) {
    $param = catFileLine("$workFile");
  } else {
    printFileLine("$workFile","$name","$param");
  }
  return $param;
}


sub dispoidinfo {
  my $ipAddr = "@_[0]";
# print("Testing $ipAddr for oid info.<P>");
# testUpdFile( fileName, httpParam, description );
  my $ipDir = getIpDir( $ipAddr );
  if ( ! -d $ipDir ) {
    print("<H3>Oops, no info found for ip address $ipAddr.</H3>");
  } else {
    my $community = catFileLine("$ipDir/snmpcomm.txt", );
#   my $community = readUpdParam("$ipDir/snmpcomm.txt", );
    my $snmpStr = "$community\@$ipAddr" . ":::::" . "1";
    my $sysName = &snmpget("$snmpStr", ".1.3.6.1.2.1.1.5.0");
    my $sysDescr = &snmpget("$snmpStr", ".1.3.6.1.2.1.1.1.0");
    my $sysOid = &snmpget("$snmpStr", ".1.3.6.1.2.1.1.2.0");
    my $oidDir = "$SIMparam{'SIMoidDir'}/.$sysOid";
    if ( ! -d $oidDir ) {
      print("oidDir $oidDir missing, will create it....\n");
      system(" mkdir -p $oidDir ");
      if ( ! -d $oidDir ) {
        print("Oops, could not create oidDir in dispoidinfo, exiting....");
	exit;
      }
    }
    my $remExecSyntax = readUpdParam("$oidDir/remExecSyntax","remExecSyntax");
    if ( "$remExecSyntax" eq "" ) { $remExecSyntax= "none"; }
    my $category = readUpdParam("$oidDir/category","category");
    if ( "$category" eq "" ) { $category = "unk"; }
    my $shortDescription = readUpdParam("$oidDir/shortDescription","shortDescription","fill");
    my $vendor = readUpdParam("$oidDir/vendor","vendor","fill");
    my $dnsPrefix = readUpdParam("$oidDir/dnsPrefix","dnsPrefix","fill");
    # radio buttons
    my $isSwitch = readUpdParam("$oidDir/isSwitch","isSwitch");
    my $getArp = readUpdParam("$oidDir/getArp","getArp");
    my $firstMacIsName = readUpdParam("$oidDir/firstMacIsName","firstMacIsName");
    my $useManualName = readUpdParam("$oidDir/useManualName","useManualName");
#   print("<PRE> community=$community, snmpStr=$snmpStr, sysName=$sysName, sysDescr=$sysDescr, sysOid=$sysOid.</PRE><P> ");
    print("
      <H3>Add Oid info for Oid $sysOid</H3>
      <FORM method=post Action=$httpScriptName?config2+dispoidinfo+$ipAddr>
      <TABLE>
        <TR><TD>OID:</TD><TD COLSPAN=2>$sysOid</TD></TR>
	<INPUT NAME=\"sysOid\" TYPE=hidden VALUE=\"$sysOid\">
	<TR><TD>Name:</TD><TD COLSPAN=2>$sysName</TD></TR>
	<TR><TD VALIGN=top>Descr:</TD><TD COLSPAN=2><PRE>$sysDescr</PRE></TD></TR>
	<TR><TD>Device description:</TD><TD><INPUT NAME=\"shortDescription\" VALUE=\"$shortDescription\" SIZE=30></TD>
	  <TD>Short device description displayed in the ip tables.</TD></TR>
	<TR><TD>Remexec syntax:</TD><TD>
	  <SELECT NAME=\"remExecSyntax\" SIZE=1>
	  <OPTION VALUE=\"$remExecSyntax\">$remExecSyntax</OPTION>");
    open( FH, $SIMparam{'SIMremExecIosTypes'} ) || die "cannot open SIMremExecIosTypes file.";
    my @lines = <FH>;
    foreach my $regel (@lines) {
      my ( $test, $type, $description ) = split(' ', $regel, 3);
      if ( "$test" eq "type" ) {
        print("<OPTION VALUE=\"$type\">$type = $description</OPTION>");
      }
    }
    print("</SELECT></TD><TD>This will define if and how can be logged in to devices.</TD></TR>
	<TR><TD>Device type:</TD><TD>
	  <SELECT NAME=\"category\" SIZE=1>
	  <OPTION VALUE=\"$category\">$category = $nodeDescription{$category} (current)</OPTION>");
    
    while ( my ($key, $value) = each(%nodeDescription) ) {
      print("<OPTION VALUE=\"$key\">$key = $value</OPTION>");
    }
    print("</SELECT>
        <TD>Define the general device type (used for color coding, logging, polling / allerting parameters etc...).</TD></TR>
	<TR><TD>DNS prefix:</TD><TD><INPUT NAME=\"dnsPrefix\" VALUE=\"$dnsPrefix\" SIZE=30></TD>
	  <TD>DNS domain that will be added to the base DNS name if no domain info is found.</TD></TR>
	<TR><TD>Device vendor:</TD><TD><INPUT NAME=\"vendor\" VALUE=\"$vendor\" SIZE=30></TD>
	  <TD>Various actions like graphing, devicemanagement etc, are split per vendor.</TD></TR>
	<TR><TD>IsSwitch:</TD><TD>");
    if ( "$isSwitch" eq "yes" ) {
      print("<INPUT NAME=\"isSwitch\" TYPE=radio VALUE=yes checked>Yes&nbsp;
             <INPUT NAME=\"isSwitch\" TYPE=radio VALUE=no>No&nbsp;");
    } else {
      print("<INPUT NAME=\"isSwitch\" TYPE=radio VALUE=yes>Yes&nbsp;
             <INPUT NAME=\"isSwitch\" TYPE=radio VALUE=no checked>No&nbsp;");
    }
    print("
        </TD><TD>If set to yes, this device will be polled fro the bridge tables (mapping mac adresses to ports)</TD></TR>
	<TR><TD>GetArp:</TD><TD>");
    if ( "$getArp" eq "yes" ) {
      print("<INPUT NAME=\"getArp\" TYPE=radio VALUE=yes checked>Yes&nbsp;
             <INPUT NAME=\"getArp\" TYPE=radio VALUE=no>No&nbsp;");
    } else {
      print("<INPUT NAME=\"getArp\" TYPE=radio VALUE=yes>Yes&nbsp;
             <INPUT NAME=\"getArp\" TYPE=radio VALUE=no checked>No&nbsp;");
    }
    print("
        </TD><TD>Should arp info be retrieved from this node (some device report arp info from out of subnet nodes)</TD></TR>
	<TR><TD>Firstmac is name:</TD><TD>");
    if ( "$firstMacIsName" eq "yes" ) {
      print("<INPUT NAME=\"firstMacIsName\" TYPE=radio VALUE=yes checked>Yes&nbsp;
             <INPUT NAME=\"firstMacIsName\" TYPE=radio VALUE=no>No&nbsp;");
    } else {
      print("<INPUT NAME=\"firstMacIsName\" TYPE=radio VALUE=yes>Yes&nbsp;
             <INPUT NAME=\"firstMacIsName\" TYPE=radio VALUE=no checked>No&nbsp;");
    }
    print("
        </TD><TD>Use this for devices with identical mac adresses (like fwsm nodes)</TD></TR>
        <TR><TD>Manual name:</TD><TD>");
    if ( "$useManualName" eq "yes" ) {
      print("<INPUT NAME=\"useManualName\" TYPE=radio VALUE=yes checked>Yes&nbsp;
             <INPUT NAME=\"useManualName\" TYPE=radio VALUE=no>No&nbsp;");
    } else {
      print("<INPUT NAME=\"useManualName\" TYPE=radio VALUE=yes>Yes&nbsp;
             <INPUT NAME=\"useManualName\" TYPE=radio VALUE=no checked>No&nbsp;");
    }
    print("
        </TD><TD>Some devices (like IBMbladecenter components) all use the samen name. Use this to have the different nodes use different names.</TD></TR>
         </TABLE>
      <INPUT TYPE=submit Value=Update>
      </FORM>
          ");
  }
}

# ------------------------------------- Main ---------------------------------------

displayMenu;

#print("Running tool=$tool, action=$action, option1=$option1, option2=$option2, option3=$option3.<P>");
if ( "$action" eq "dispoidinfo" ) {
  dispoidinfo( $option1 );
} else {
  print("<H3>Oops, action $action not defined in the config tool.</H3>");
}
